Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

735
Vistas
Can't get data from client in FastAPI websocket | Python

I get this error in my fastapi log:

INFO:     ('172.28.0.1', 35088) - "WebSocket /ws" [accepted]
INFO:     connection open
ERROR:    Exception in ASGI application
...
    await self.app(scope, receive, send)
    File "/usr/local/lib/python3.9/site-packages/starlette/exceptions.py", line 58, in __call__
    await self.app(scope, receive, send)
    File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
    File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 297, in handle
    await self.app(scope, receive, send)
    File "/usr/local/lib/python3.9/site-packages/starlette/routing.py", line 68, in app
    await func(session)
    File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 273, in app
    await dependant.call(**values)
    File "/code/./app/main.py", line 127, in websocket_endpoint
    data = await websocket.receive_text()
     File "/usr/local/lib/python3.9/site-packages/starlette/websockets.py", line 85, in receive_text
     self._raise_on_disconnect(message)
    File "/usr/local/lib/python3.9/site-packages/starlette/websockets.py", line 80, in _raise_on_disconnect
    raise WebSocketDisconnect(message["code"])
    starlette.websockets.WebSocketDisconnect: 1000
    INFO:     connection closed

I can't find the problem. Why my fastapi route can't get the data from client but the server message to client works.

Client Code:

import asyncio
from time import sleep
import websockets

async def get_work():
    async with websockets.connect("ws://localhost:3000/ws", open_timeout=None) as websocket:
        await websocket.send('Hello from CLient')
        answer = await websocket.recv()
        print(answer)
         
while True:
    asyncio.run(get_work())
    sleep(5)

Server Code:

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        try:
            data = await websocket.receive_text()
            print(data)
        except RuntimeError:
            break
        await websocket.send_text("Hello from Server") 

Thanks a lot for help!

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda